/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package multiversiontechnicaldocumentation;

import java.awt.GraphicsConfiguration;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

/**
 *
 * @author VadimBaitoiu
 */
public class MainFrame extends javax.swing.JFrame {

    private Document document;

    public void setDocument(Document document) {
        this.document = document;
    }

    /**
     * Creates new form MainFrame
     *
     * @param graphicConf
     */
    public MainFrame(GraphicsConfiguration graphicConf) {
        super(graphicConf);
        this.setTitle("Multiversion technical document editor");
        this.setFocusable(true);
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        menuBar = new javax.swing.JMenuBar();
        menuFile = new javax.swing.JMenu();
        menuItemFileNew = new javax.swing.JMenuItem();
        menuItemFileOpen = new javax.swing.JMenuItem();
        menuEdit = new javax.swing.JMenu();
        menuTest = new javax.swing.JMenu();
        menuItemGenerate = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        menuFile.setText("File");

        menuItemFileNew.setText("New");
        menuFile.add(menuItemFileNew);

        menuItemFileOpen.setText("Open...");
        menuItemFileOpen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuItemFileOpenActionPerformed(evt);
            }
        });
        menuFile.add(menuItemFileOpen);

        menuBar.add(menuFile);

        menuEdit.setText("Edit");
        menuBar.add(menuEdit);

        menuTest.setText("Test");

        menuItemGenerate.setText("Generate");
        menuItemGenerate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuItemGenerateActionPerformed(evt);
            }
        });
        menuTest.add(menuItemGenerate);

        menuBar.add(menuTest);

        setJMenuBar(menuBar);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 279, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void menuItemGenerateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuItemGenerateActionPerformed

    }//GEN-LAST:event_menuItemGenerateActionPerformed

    private void menuItemFileOpenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuItemFileOpenActionPerformed
        // Create a file chooser
        final JFileChooser fc = new JFileChooser();

        // In response to a button click
        int returnVal = fc.showOpenDialog(this);

        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File file = fc.getSelectedFile();

            try {
                Reader reader = new Reader();
                this.document = reader.read(file);
            } catch (    DocumentCommandException | DocumentStructureException ex) {
                JOptionPane.showMessageDialog(this, ex.getMessage(), ex.getClass().
                        toString(), JOptionPane.ERROR_MESSAGE);
            } catch (FileNotFoundException ex) {
                JOptionPane.showMessageDialog(this, ex.getMessage(), ex.getClass().
                        toString(), JOptionPane.ERROR_MESSAGE);
            } catch (IOException ex) {
                JOptionPane.showMessageDialog(this, ex.getMessage(), ex.getClass().
                        toString(), JOptionPane.ERROR_MESSAGE);
            }
        }
    }//GEN-LAST:event_menuItemFileOpenActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JMenu menuEdit;
    private javax.swing.JMenu menuFile;
    private javax.swing.JMenuItem menuItemFileNew;
    private javax.swing.JMenuItem menuItemFileOpen;
    private javax.swing.JMenuItem menuItemGenerate;
    private javax.swing.JMenu menuTest;
    // End of variables declaration//GEN-END:variables
}
